package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baor.dao.ClassSubjectDao;
import com.baor.dao.SubjectDao;
import com.baor.dao.TeacherClassDao;
import com.baor.dao.TeacherDao;
import com.baor.domain.ClassSubjectVo;
import com.baor.domain.SubjectVo;
import com.baor.domain.TeacherClassVo;
import com.baor.domain.TeacherVo;
import com.baor.service.SubjectService;
import com.baor.utils.AjaxResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static com.baor.enums.Results.*;
/**
 * 学科，科目具有方法
 * 列表，增加，删除，修改
 */

@Service
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    private SubjectDao subjectDao;

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private ClassSubjectDao classSubjectDao;

    @Autowired
    private TeacherClassDao teacherClassDao;

    /**
     * 返回学科列表集合
     * @return
     */

    @Override
    public AjaxResult<?> list() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<SubjectVo> list = subjectDao.selectList(queryWrapper);
        return AjaxResult.success(list);
    }

    /**
     * 不可重复插入学科名称
     * @param subjectVo 前端接受参数
     * @return
     */

    @Override
    public AjaxResult<?> insert(SubjectVo subjectVo) {
        UpdateWrapper wrapper = new UpdateWrapper();
        List<SubjectVo> list = subjectDao.selectList(wrapper);
        for ( SubjectVo vo : list ){
            if (Strings.isNotBlank(subjectVo.getSubjectName()) && subjectVo.getSubjectName().equals(vo.getSubjectName()) ){
                return AjaxResult.error(INSERT_ERROR);
            }
        }
        subjectDao.insert(subjectVo);
        return AjaxResult.success(INSERT_SUCCESS);
    }

    /**
     * 接收数组（学科id），来删除学科
     * @param list
     * @return
     */

    @Override
    public AjaxResult<?> delete(List<Integer> list) {
        int i = subjectDao.deleteBatchIds(list);
        return AjaxResult.success( i > 0 ? DELETE_SUCCESS : DELETE_ERROR );
    }

    /**
     * 传入班级id,查存在哪些课程
     * 返回各学科下有什么老师
     * 实现逻辑:查出所有学科,循环查出学科下的老师信息
     * @return
     */
    @Override
    public AjaxResult<?> teachers(Integer classId) {
        List<Map<String,Object>> list = new ArrayList<>();
        LambdaQueryWrapper<ClassSubjectVo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClassSubjectVo::getClassId,classId);
        List<ClassSubjectVo> classSubjectVos = classSubjectDao.selectList(wrapper);
        //classSubjectVos class - subject 开设学科
        for ( ClassSubjectVo vo : classSubjectVos ){//vo.getSubject() 开设课程
            System.out.println("begin-----------------");
            LambdaQueryWrapper<TeacherClassVo> rapper = new LambdaQueryWrapper<>();
            rapper.eq(TeacherClassVo::getSubjectId,vo.getSubjectId())
                    .eq(TeacherClassVo::getClassId,vo.getClassId());
            List<Integer> collect = teacherClassDao.selectList(rapper)
                        .stream()
                        .map(x -> Integer.valueOf(x.getTeacherId()))
                        .collect(Collectors.toList());//班级下 学科对应存在老师及老师对应ids
            System.out.println("collect:"+collect);
            if ( collect.size() == 0 ){
                Map<String,Object> res = new HashMap<>();
                res.put("subject",subjectDao.selectById(vo.getSubjectId()));
                list.add(res);
                continue;
            }
            LambdaQueryWrapper<TeacherVo> mapper = new LambdaQueryWrapper<>();
            mapper.in(TeacherVo::getTeacherId,collect);
            List<TeacherVo> teacherVos = teacherDao.selectList(mapper);
            Map<String,Object> res = new HashMap<>();
            res.put("subject",subjectDao.selectById(vo.getSubjectId()));
            res.put("teachers",teacherVos);
            list.add(res);
        }
        return AjaxResult.success(list);
    }

    /**
     * 为课程添加老师,入参：老师ids，出参success
     * @param map
     * @return
     */
    @Override
    public AjaxResult<?> teachersInsert(Map<String,Object> map) {
        System.out.println(map);
        String subject = map.get("subjectId").toString();
        String i = map.get("teachers").toString();
        TeacherClassVo teacherClassVo = new TeacherClassVo();
        teacherClassVo.setSubjectId(subject);
        teacherClassVo.setTeacherId(i);
        teacherClassDao.insert(teacherClassVo);
        return AjaxResult.success(INSERT_SUCCESS);
    }
}
